package com.violet.base.server.dic.controller;

import com.violet.base.api.dic.dto.DicItemDTO;
import com.violet.base.server.dic.service.DictItemService;
import com.violet.common.response.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @Author: wqf
 * @Date: 2022/10/27
 * @Description:
 */
@Tag(name = "字典数据模块", description = "字典数据模块")
@RestController
@RequestMapping("/dict/item")
public class DictItemController {

    @Resource
    private DictItemService dictItemService;

    @Operation(summary = "新增字典数据")
    @PostMapping
    public Result<Void> addDictItem(@ParameterObject DicItemDTO dicItemDTO) {
        dictItemService.saveOrUpdateDictItem(dicItemDTO);
        return Result.succeed();
    }

    @Operation(summary = "更新字典数据")
    @PutMapping
    public Result<Void> updateDictItem(DicItemDTO dicItemDTO) {
        dictItemService.saveOrUpdateDictItem(dicItemDTO);
        return Result.succeed();
    }

    @Operation(summary = "根据主键id删除字典数据")
    @DeleteMapping("/{id}")
    public Result<Void> deleteDictItemById(@Parameter(description = "主键id")@PathVariable Long id) {
        dictItemService.deleteDictItemById(id);
        return Result.succeed();
    }

    @Operation(summary = "根据字典分类编码查询字典数据列表")
    @GetMapping("/type/{dicTypeCode}")
    public Result<List<DicItemDTO>> findDictItemByDicTypeCode(@Parameter(description = "字典分类编码") @PathVariable String dicTypeCode) {
        return Result.succeed(dictItemService.findDictItemByDicTypeCode(dicTypeCode));
    }


    @Operation(summary = "根据字典分类编码查询字典数据下拉列表")
    @GetMapping("/drop_down_list/{dicTypeCode}")
    public Result<LinkedList<Map<String, String>>> findDictItemDropDownListByDicTypeCode(@Parameter(description = "字典分类编码")@PathVariable String dicTypeCode) {
        return Result.succeed(dictItemService.findDictItemDropDownListByDicTypeCode(dicTypeCode));
    }
}
